/** layuiAdmin.std-v1.2.1 LPPL License By http://www.layui.com/admin/ */
layui.define(['form', 'pkc', 'format', 'validator'], function (e) {
  var t = layui.$;
  var layform = layui.form;
  var pkc = layui.pkc;
  var format = layui.format;
  var setter = layui.setter;
  var validator = layui.validator;

  var form = {
    local: {
      fm: null //当前页面form元素
    },
    /**
     * 初始化控件事件
     * */
    initEv: function () {
      //监听提交
      layform.on("submit(fmsub)", function (fm) {
        layform.verify();
        var emailreg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        var emailadd = form.getWebParam(fm).emailadd;
        var emails = emailadd.replace("；",";").split(";");
        var name = [];
        var data = "<ul><li style='list-style:none'>"+
                  "<span style='display:inline-block;width:20%'>停车场名称</span>"+
                  "<span style='display:inline-block;width:20%'>停车场id</span>"+
                  "<span style='display:inline-block;width:20%'>key</span>"+
                  "</li>";
        var datacheck = "";
        for(var i = 0; i < emails.length; i++){
          if(!emailreg.test(emails[i])){
            layer.alert("邮箱地址格式不正确");
            return false;
          }
        }
        for(var i = 0; i < parent.checkdata.length; i++){
          name.push(parent.checkdata[i]['parkingname']);
          datacheck += "<li style='list-style:none'><span style='display:inline-block;width:20%'>" + parent.checkdata[i]['parkingname'] + 
                        "</span><span style='display:inline-block;width:20%'>" + parent.checkdata[i]['parkingid'] +
                        "</span><span style='display:inline-block;width:20%'>" + parent.checkdata[i]['signkey'] +
                        "</span></li>";
        }
        name = name.join(',');
        data = data + datacheck + '</ul>';
        form.toolAction.send(emails, data, name);          
        return false;
      });

      t('#btnBack').click(function (e) {
        form.close();
        e.preventDefault();
      });

    },
    /**
     * 界面初始化
     * */
    init: function () {
      // layer.load(2);
      this.initEv();
      layer.closeAll("loading");
    },

    toolAction: {
      // 发送
      send: function (email, data, name) {
        var loading= layer.load("1");
        var a = {};
            a['email'] = email;
            a['emaildata'] = data;
            a['theme'] = name;
         return pkc.http.SendPost(a, 'psamcloud.boss.device.lane.sendmail', function (ret) {
          var ret = JSON.parse(ret);
            if (ret) {
              if (ret.Status === "1") {
                //success
                layer.msg('发送成功');
                form.close();
              } else {
                layer.alert(ret.Message);
              }
            }
            layer.close(loading);
          }, function (ret) {
            layer.alert("网络异常.请稍后再试！");
            layer.close(loading);
          })
      }

    },
    getWebParam: function (fmData) {
      var data = {};
      data["emailadd"] = fmData.field.emailurl;
      return data;
    },
    /**
     * 关闭窗体
     */
    close: function () {
      var index = parent.layer.getFrameIndex(window.name);
      setTimeout(function () {
        parent.layer.close(index);
      }, 1000)
    }
  };

  e("sendemail", {
    init: () => {
      form.init();
    }
  });
});